
from googletrans import Translator
# from abstract.extract_info import extract_keys ,extract_sents
from abstract.extract_infov3 import extract_keys ,extract_sents

def translate_keys_sents(sentence):
    """
        将英文标题翻译成中文并且分词，返回同样的keys ，sents，保持接口一致
        如果有网则进行翻译，否则只能够输出英文的摘要和关键字
    """
    try :
       
        translator = Translator(service_urls=['translate.google.cn'], proxies={'http': "localhost:80"})
        chinese_sentence = translator.translate(sentence, src='en', dest='zh-cn').text

        keys_zh = extract_keys(chinese_sentence)
        sents_zh = chinese_sentence

        keys = keys_zh 
        sents = sents_zh
    except:
        keys = extract_keys(sentence)
        sents = sentence


    return keys , sents


def translate_zh(sentence, english=False):
    """
        对于任何字符串，如果有网则进行翻译
        如果english为true，则同时返回英文版本
    """

    #  判断是否联网 ， 如果为0则是有网
    try :
        translator = Translator(service_urls=['translate.google.cn'], proxies={'http': "localhost:80"})
        chinese_sentence = translator.translate(sentence, src='en', dest='zh-cn').text
        sents = chinese_sentence

    except:
        sents = sentence

    if english:
        return "{}\n{}".format(sents , sentence)
    else:
        return "{}".format(sents)

# if __name__ == '__main__':

#     sentence = "a street sign that reads `` <UNK> <UNK> '' on the side of a building .\na street sign that reads `` <UNK> <UNK> '' on the side of a building"

#     print(translate_zh(sentence , english=True))

